package com.acwing.partition12;

import java.io.*;
import java.util.ArrayDeque;
import java.util.Queue;

/**
 * @author `RKC`
 * @date 2022/1/23 13:57
 */
public class AC1172祖孙询问 {

    private static final int N = 40010, M = 2 * N, D = (int) (Math.log(N) / Math.log(2));
    private static int[] head = new int[N], element = new int[M], next = new int[M];
    private static int[] depth = new int[N];
    private static int[][] f = new int[N][D + 1];

    private static int n = 0, idx = 0;
    private static Queue<Integer> queue = new ArrayDeque<>(N);

    private static final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    private static final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));

    public static void main(String[] args) throws IOException {
        n = Integer.parseInt(reader.readLine());
        int root = -1;
        for (int i = 0; i < N; i++) head[i] = -1;
        for (int i = 0; i < n; i++) {
            String[] s = reader.readLine().split("\\s+");
            int u = Integer.parseInt(s[0]), v = Integer.parseInt(s[1]);
            if (v == -1) root = u;
            else {
                add(u, v);
                add(v, u);
            }
        }
        //预处理depth和f数组
        bfs(root);
        int q = Integer.parseInt(reader.readLine());
        while (q-- > 0) {
            String[] s = reader.readLine().split("\\s+");
            int u = Integer.parseInt(s[0]), v = Integer.parseInt(s[1]);
            int ans = lca(u, v);
            if (ans == u) writer.write(1 + "\n");
            else if (ans == v) writer.write(2 + "\n");
            else writer.write(0 + "\n");
        }
        writer.flush();
    }

    private static void bfs(int root) {
        //预处理每个节点的深度和f数组，时间复杂度O(nlogn)
        queue.add(root);
        depth[root] = 1;
        while (!queue.isEmpty()) {
            int u = queue.poll();
            for (int i = head[u]; i != -1; i = next[i]) {
                int v = element[i];
                if (depth[v] != 0) continue;
                depth[v] = depth[u] + 1;
                f[v][0] = u;
                for (int j = 1; j <= D; j++) {
                    f[v][j] = f[f[v][j - 1]][j - 1];
                }
                queue.add(v);
            }
        }
    }

    private static int lca(int x, int y) {
        //定义x的深度要大于y的深度，否则交换
        if (depth[x] < depth[y]) {
            int t = x;
            x = y;
            y = t;
        }
        //因为x的深度要大于y的深度，所以先将x跳到和y同一层
        for (int i = D; i >= 0; i--) {
            if (depth[f[x][i]] >= depth[y]) x = f[x][i];
        }
        //此时x和y在同一层
        if (x == y) return x;
        //一起跳到LCA的下面一层
        for (int i = D; i >= 0; i--) {
            if (f[x][i] != f[y][i]) {
                x = f[x][i];
                y = f[y][i];
            }
        }
        return f[x][0];
    }

    private static void add(int u, int v) {
        element[idx] = v;
        next[idx] = head[u];
        head[u] = idx++;
    }
}
